package com.ysq.www.api.work.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ysq.www.api.work.entity.GeneralTask;
import com.ysq.www.api.work.service.IGeneralTaskService;
import com.ysq.www.common.annotation.AuthToken;
import com.ysq.www.common.result.ResultMsg;
import com.ysq.www.common.utils.Comm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 通用任务表 前端控制器
 * </p>
 *
 * @author dql
 * @since 2020-08-11
 */
@RestController
@RequestMapping("/work/generalTask")
public class GeneralTaskController {

    @Autowired
    private IGeneralTaskService generalTaskService;

    /**
     * 查询我发起的通用任务详情
     * @param gtId 通用任务id
     * @return
     */
    @GetMapping("/detail")
    @AuthToken
    public ResultMsg selectDetailById(@RequestParam(required = true) int gtId){
        GeneralTask gt = generalTaskService.selectDetailById(gtId);
        return ResultMsg.SUCCESS.setNewMsg("查询成功").setNewData(gt);
    }

    /**
     * 查询待处理/已处理/抄送通用任务详情
     * @param gtId 通用任务id
     */
    @GetMapping("/dispose")
    @AuthToken
    public ResultMsg selectDisposeById(@RequestParam(required = true) int gtId){
        GeneralTask gt = generalTaskService.selectDisposeById(gtId);
        return ResultMsg.SUCCESS.setNewMsg("查询成功").setNewData(gt);
    }


    /**
     * 提交通用任务
     * @param gk 通用任务对象
     * @return
     */
    @PostMapping("/add")
    @AuthToken
    public ResultMsg addGeneralTask(@RequestBody GeneralTask gk){
        ResultMsg rm = generalTaskService.addGeneralTask(gk);
        return rm;
    }

    /**
     * 分页查询我发起的通用任务
     * @param userId 发起人用户id
     * @param pageNumber 页码
     * @param pageSize 页大小
     * @param time 日期
     * @return
     */
    @GetMapping("/sponsor")
    @AuthToken
    public ResultMsg selectSponsorById(@RequestParam(required = true) int userId,
                                       @RequestParam(required = true) int pageNumber,
                                       @RequestParam(required = true) int pageSize,String time){
        //if (StringUtils.isEmpty(time)) time = Comm.getSysDate(7);
        IPage<GeneralTask> gPage = generalTaskService.selectSponsorById(userId,pageNumber,pageSize,time);
        return ResultMsg.SUCCESS.setNewMsg("查询成功").setNewData(gPage);
    }


    /**
     * 分页查询待处理/已处理通用任务
     * @param userId 审批人用户id
     * @param type 类型：0未处理，1已处理
     * @param pageNumber 页码
     * @param pageSize 页大小
     * @param time 日期
     * @return
     */
    @GetMapping("/pending")
    @AuthToken
    public ResultMsg selectPendingById(@RequestParam(required = true) int userId,
                                       @RequestParam(required = true) int type,
                                       @RequestParam(required = true) int pageNumber,
                                       @RequestParam(required = true) int pageSize,String time){
        //if (StringUtils.isEmpty(time)) time = Comm.getSysDate(7);
        IPage<GeneralTask> gPage = generalTaskService.selectPendingByPage(userId,type,pageNumber,pageSize,time);
        return ResultMsg.SUCCESS.setNewMsg("查询成功").setNewData(gPage);
    }


    /**
     * 处理通用任务请求
     * @param gtId 通用任务id
     * @param result 处理结果：1通过，2驳回
     * @return
     */
    @GetMapping("/check")
    @AuthToken
    public ResultMsg checkGeneralTaskById(@RequestParam(required = true) int gtId,
                                          @RequestParam(required = true) int result){
        ResultMsg rm = generalTaskService.checkGeneralTaskById(gtId,result);
        return rm;
    }

    /**
     * 撤销通用任务
     * @param gtId 通用任务id
     * @return
     */
    @GetMapping("/repeal")
    @AuthToken
    public ResultMsg repealGeneralTaskById(@RequestParam(required = true) int gtId){
        ResultMsg rm = generalTaskService.repealGeneralTaskById(gtId);
        return rm;
    }

    /**
     * 分页查询抄送至我的通用任务
     * @param userId 用户id
     * @param pageNumber 页码
     * @param pageSize 页大小
     * @param time 日期
     * @return
     */
    @GetMapping("/copyTo")
    @AuthToken
    public ResultMsg selectCopyToMeByPage(@RequestParam(required = true) int userId,
                                          @RequestParam(required = true) int pageNumber,
                                          @RequestParam(required = true) int pageSize,String time){
        //if (StringUtils.isEmpty(time)) time = Comm.getSysDate(7);
        IPage<GeneralTask> gPage = generalTaskService.selectCopyToMeByPage(userId,pageNumber,pageSize,time);
        if (gPage == null) return ResultMsg.SUCCESS.setNewMsg("查询成功,数据为空");
        return ResultMsg.SUCCESS.setNewMsg("查询成功").setNewData(gPage);
    }


}
